import {View} from "@tarojs/components";
import {useEffect, useRef, useState} from "react";
import {IdParams, ReportModel} from "@/common/apis/model";
import Utils from "@/utils/common";
import JsonUtils from "@/utils/json_utils";
import {Button, Cell, Empty} from "@antmjs/vantui";

definePageConfig({
  navigationBarTitleText: '检测结果'
})

export default function Test_result() {
  const testResult = useRef<IdParams>();

  const [reportItem, setReportItem] = useState<ReportModel[]>()


  useEffect(() => {
    const p = Utils.getParamsByRouter(Utils.getI())
    if (p && p.data) {
      testResult.current = Utils.getObjectWithUrl<IdParams>(p.data)
      const result = JsonUtils.parse<Array<{
        name: string;
        response: {
          data: { name: string; url: string };
        };
      }>>(testResult.current?.id)

      const processedResult = result.map(item => {
        // 取出 name 和 response 里的 data 字段
        const rI: ReportModel = {
          title: item.name,
          name: item.response.data.name,
          url: item.response.data.url
        }
        return rI;
      });
      setReportItem(processedResult)
    }
  }, []);

  return (
    <View>
      {
        reportItem ? reportItem?.map((item, index) => {
          return (
            <View key={index}>
              <Cell title={item.title}>
                <Button type='info' onClick={() => {
                  Utils.scanFile(item.url)
                }}
                >下载</Button>
              </Cell>
            </View>
          )
        }) : <Empty description='暂无数据'/>
      }
    </View>
  )
}


